package cc.mrbird.febs.system.service.impl;

import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.SortUtil;
import cc.mrbird.febs.system.entity.DataDictionary;
import cc.mrbird.febs.system.mapper.DataDictionaryMapper;
import cc.mrbird.febs.system.service.IDataDictionaryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service("DataDictionaryService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryMapper, DataDictionary> implements IDataDictionaryService {
    @Override
    public DataDictionary findDataDictionary(Long dataDictionaryId) {
        return this.getById(dataDictionaryId);
    }

    @Override
    public IPage<DataDictionary> findDataDictionarys(QueryRequest request, DataDictionary dataDictionary) {
        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(dataDictionary.getContent())) {
            queryWrapper.like(DataDictionary::getContent, dataDictionary.getContent());
        }

        Page<DataDictionary> page = new Page<>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, "content", FebsConstant.ORDER_DESC, true);
        return this.page(page, queryWrapper);
    }

    @Override
    @Transactional
    public void createDataDictionary(DataDictionary dataDictionary) {
        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dataDictionary.getContent())) {
            queryWrapper.eq(DataDictionary::getContent, dataDictionary.getContent());
        }
        int count=count(queryWrapper);
        //查找是否有content一样的数据字典条目，若有相同的不save，没有相同的才save
        if(count==0){
            this.save(dataDictionary);
        }

    }

    @Override
    @Transactional
    public void updateDataDictionary(DataDictionary dataDictionary) {
        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dataDictionary.getContent())) {
            queryWrapper.eq(DataDictionary::getContent, dataDictionary.getContent());
        }
        int count=count(queryWrapper);
        //查找是否有content一样的数据字典条目，若有相同的不update，没有相同的才update
        if(count==0){
            updateById(dataDictionary);
        }

    }

    @Override
    @Transactional
    public void deleteDataDictionarys(String[] dataDictionaryIds) {
        List<String> list = Arrays.asList(dataDictionaryIds);
        this.baseMapper.deleteBatchIds(list);
    }

    @Override
    @Transactional
    public void addDataDictionaryContents(String[] contents) {
        List<String> list = Arrays.asList(contents);
        list.forEach(o->
        {DataDictionary dataDictionary=new DataDictionary();
         dataDictionary.setDataDictionaryId(null);
         dataDictionary.setContent(o);
         createDataDictionary(dataDictionary);
        });
    }
}
